<template>
  <div class="com-container">
    <div class="com-chart" ref="seller_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  data() {
    return {
      chartInstance: null,
      allData: [],
      currentPage: 1,
      totalPage: 0,
      timerId: null,
    }
  },
  computed: {
    ...mapState(['theme']),
  },
  watch: {
    theme() {
      this.chartInstance.dispose()
      this.initChart()
      this.screenAdapter()
      this.updateChart()
    },
  },
  created() {
    this.$socket.registerCallBack('sellerData', this.getData)
  },
  async mounted() {
    this.initChart()
    // this.getData()
    this.$socket.send({
      action: 'getData',
      socketType: 'sellerData',
      chartName: 'seller',
    })
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()

    // const { data } = await this.$http.get('trend')
    // console.log('trend', data)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timerId)
    this.$socket.unRegisterCallBack('stockData')
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.seller_ref, this.theme)
      const initOption = {
        title: {
          text: '┃ SELL STATISTICS',
          left: 20,
          top: 20,
          textStyle: {
            // color: 'white',
          },
        },
        grid: {
          top: '20%',
          left: '3%',
          right: '6%',
          bottom: '3%',
          containLabel: true,
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            z: 0,
            lineStyle: {
              color: '#2d3443',
            },
          },
        },
        yAxis: {
          type: 'category',
        },
        xAxis: {
          type: 'value',
        },
        series: [
          {
            type: 'bar',
            label: {
              show: true,
              position: 'right',
              color: 'white',
            },
            itemStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [
                  {
                    offset: 0,
                    color: '#5052ee', // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: '#ab6ee5', // 100% 处的颜色
                  },
                ],
              },
            },
          },
        ],
      }
      this.chartInstance.setOption(initOption)
      // 监听鼠标事件
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.chartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    async getData() {
      const { data } = await this.$http.get('seller')
      // console.log('data', data)
      this.allData = data
      // 从小到大排序
      this.allData = this.allData.sort((a, b) => {
        return a.value - b.value
      })
      // 两种计算总页数的方式
      // this.totalPage =
      //   this.allData.length % 5 === 0
      //     ? this.allData.length / 5
      //     : parseInt(this.allData.length / 5) + 1
      this.totalPage = Math.ceil(this.allData.length / 5)
      this.updateChart()
      this.startInterval()
    },
    updateChart() {
      const start = (this.currentPage - 1) * 5
      const end = this.currentPage * 5
      const showData = this.allData.slice(start, end)

      const sellerNames = showData.map((item) => {
        return item.name
      })
      const sellerValues = showData.map((item) => {
        return item.value
      })
      const updateOption = {
        xAxis: {
          // 固定x轴的最大值
          max: this.allData[this.allData.length - 1].value,
        },
        yAxis: {
          data: sellerNames,
        },
        series: [
          {
            data: sellerValues,
          },
        ],
      }
      this.chartInstance.setOption(updateOption)
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.currentPage++
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1
        }
        this.updateChart()
      }, 3000)
    },
    screenAdapter() {
      const sizeValue = (this.$refs.seller_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: sizeValue,
            },
          },
        },
        series: [
          {
            barWidth: sizeValue,
            itemStyle: {
              barBorderRadius: [0, sizeValue / 2, sizeValue / 2, 0],
            },
          },
        ],
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
  },
}
</script>

<style></style>
